package com.qhgrain.app.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paas.base.modules.utils.lang.StringUtil;
import com.paas.base.modules.utils.page.PageBean;
import com.pubframework.common.http.HttpUtil;
import com.qhgrain.app.constant.UrlEnum;
import com.qhgrain.app.syncto4Ainfo.SynclogAppInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogRoleInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogUnitInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogUserInfo;
import com.qhgrain.app.util.ConfigUtil;
import com.qhgrain.app.vo.SynclogVo;

@Service
public class SynclogService {
	private static Logger LOG = LoggerFactory.getLogger(SynclogService.class);

	private static final String Base_Url = ConfigUtil.getProperties(UrlEnum.SYNCLOG)+"/synclog/1.0.0";

	public PageBean<SynclogVo> queryPage(String dataName,String dataType, String operationType, String syncResult,String pageNo,String pageSize){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		if(StringUtil.isNotBlank(dataName)){
			paramMap.put("dataName", dataName);
		}
		if(StringUtil.isNotBlank(dataType)){
			paramMap.put("dataType", dataType);
		}
		if(StringUtil.isNotBlank(operationType)){
			paramMap.put("operationType", operationType);
		}
		if(StringUtil.isNotBlank(syncResult)){
			paramMap.put("syncResult", syncResult);
		}
		if(StringUtil.isBlank(pageNo)){
			pageNo = "1";
		}
		if(StringUtil.isBlank(pageSize)){
			pageSize = "10";
		}
		paramMap.put("pageNo", pageNo);
		paramMap.put("pageSize", pageSize);
		
		PageBean<SynclogVo> pageBean = new PageBean<SynclogVo>(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		try {
			String result = HttpUtil.post(Base_Url+"/synclog/queryPage", paramMap);
			System.out.println(result);
			System.out.println("Base_Url:"+Base_Url);
			JSONObject resJson = JSONObject.parseObject(result);
			if("1".equals(resJson.getString("state"))){
				JSONObject dataJson = resJson.getJSONObject("data");
				pageBean.setData(JSONObject.parseArray(dataJson.getString("list"), SynclogVo.class));
				pageBean.setTotalCount(dataJson.getIntValue("totalCount"));
			}else{
				LOG.error("日志分页查询失败"+ resJson);
			}
			
		} catch (IOException e) {
			LOG.error("分页查询API出错:"+e.getMessage());
		}
		return pageBean;
	}
	
	/*public List<SynclogVo> getAllFailSynclog(){
		List<SynclogVo> dataList = null;
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("syncResult", "fail");
		try {
			String result = HttpUtil.post(Base_Url+"/synclog/queryPage", paramMap);
			System.out.println(result);//
			JSONObject resJson = JSONObject.parseObject(result);
			if("1".equals(resJson.getString("state"))){
				JSONObject dataJson = resJson.getJSONObject("data");
				dataList = JSONObject.parseArray(dataJson.getString("list"), SynclogVo.class);
			}else{
				LOG.error("查询同步失败日志出错"+ resJson);
			}
			
		} catch (IOException e) {
			LOG.error("分页查询API出错:"+e.getMessage());
		}
		return dataList;
	}*/
	
	public void addSynclog(String dataId,String dataName, String dataType, String operationType, String objInfo, String syncResult){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		
		paramMap.put("dataId", dataId);
		paramMap.put("dataName",dataName);
		paramMap.put("dataType", dataType);
		paramMap.put("operationType", operationType);
		paramMap.put("objInfo", objInfo);
		paramMap.put("syncResult", syncResult);
		
		try {
			String result = HttpUtil.post(Base_Url+"/synclog/add", paramMap);
			
			JSONObject resJson = JSONObject.parseObject(result);
			if(!"1".equals(resJson.getString("state"))){
				LOG.error("日志添加失败："+resJson.getString("msg"));
			}
		} catch (Exception e) {
			LOG.error("日志API出错："+e.getMessage());
		}
	}
	
	public void updateSyncResult(String synclogId,String syncResult){
		String url = Base_Url + "/synclog/updateSyncResult";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("synclogId", synclogId);
		paramMap.put("syncResult", syncResult);
		try {
			String vo = HttpUtil.post(url, paramMap);
		} catch (IOException e) {
			LOG.error(e.getMessage());
		}
	}
	
	public void getResyncData(List<SynclogRoleInfo> syncRoles,List<SynclogAppInfo> syncApps,List<SynclogUserInfo> syncUsers,List<SynclogUnitInfo> syncUnits) throws Exception{
		
			String vo = HttpUtil.post(Base_Url+"/synclog/getResynclogData", new HashMap<String,Object>());
			JSONObject voJson = JSONObject.parseObject(vo);
			if("1".equals(voJson.getString("state"))){
				JSONObject dataJson = JSONObject.parseObject(voJson.getString("data"));
				syncRoles.addAll(JSONArray.parseArray(dataJson.getString("syncRoles"), SynclogRoleInfo.class));
				syncApps.addAll(JSONArray.parseArray(dataJson.getString("syncApps"),SynclogAppInfo.class));
				syncUsers.addAll(JSONArray.parseArray(dataJson.getString("syncUsers"),SynclogUserInfo.class));
				syncUnits.addAll(JSONArray.parseArray(dataJson.getString("syncUnits"),SynclogUnitInfo.class));
			}else{
				throw new Exception(voJson.getString("msg"));
			}
	}
}
